﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ru.yourpictures.Core.Imaging.Filters
{
    [FilterImplementation(GdiPlusImplemented = true, LowLevelImplemented = false)]
    public class Overlap : FilterBase
    {
        private Point _leftTopAnchor = new Point(0, 0);
        private Bitmap _overlapImage = null;

        #region Public properties
        public Bitmap OverlapImage
        {
            get { return _overlapImage; }
            set { _overlapImage = value; }
        }

        public Point LeftTopAnchor
        {
            get { return _leftTopAnchor; }
            set { _leftTopAnchor = value; }
        }
        #endregion

        #region Ctor
        public Overlap(Bitmap overlapImage, int anchorLeft, int anchorTop)
        {
            _overlapImage = overlapImage;
            _leftTopAnchor.X = anchorLeft;
            _leftTopAnchor.Y = anchorTop;
        }
        public Overlap(Bitmap overlapImage, Point leftTopAnchor)
        {
            _overlapImage = overlapImage;
            _leftTopAnchor = leftTopAnchor;
        }
        public Overlap() { }
        #endregion

        #region Not in use.
        protected override void FilterImplementation()
        {
            if (_pixelFormat != _overlapImage.PixelFormat)
            {
                throw new ArgumentException("Pixel format for canvas image and overlap image does not match");
            }

            _BitmapInfo bm = new _BitmapInfo(_overlapImage);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    int srcIndex = y * bm.Stride + x * bm.BytesPerPixel;
                    int dstIndex = (y + _leftTopAnchor.Y) * _stride + (x + _leftTopAnchor.X) * _bytesPerPixel;
                    CopyPixel(srcIndex, bm.Pixels, dstIndex, _pixels, _bytesPerPixel);
                }
            }
        }
        #endregion

        public override Bitmap Convert(Bitmap b)
        {
            Bitmap bm = new Bitmap(b);
            Graphics g = Graphics.FromImage(bm);
            g.DrawImage(_overlapImage, _leftTopAnchor);
            return bm;
        }

        public override void Apply(Bitmap b)
        {
            Graphics g = Graphics.FromImage(b);
            g.DrawImage(_overlapImage, _leftTopAnchor);
        }
    }
}
